using System;
using System.Collections.Generic;
using System.Text;
using GarageGames.Torque.GUI;
using GarageGames.Torque.Sim;
using Microsoft.Xna.Framework.Input;
using GarageGames.Torque.Core;
using GarageGames.Torque.T2D;
using GarageGames.Torque.XNA;
using GarageGames.Torque.Platform;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Xyne.Network;
using Xyne.Utilities;
using Xyne.Events;
using Xyne.GUI.GUIControls.Menu;

namespace Xyne.GUI
{
    public class PauseMenuGUI : GUIControl, IGUIScreen
    {
        #region Variables
        InputMap inputMapHolder;
        InputMap pauseInputMap;

        GUIBitmap backgroundBitmap;

        bool menuActive;

        private String gamer;

        private MenuManager menuManager;
        private CircleMenu pauseMenu;
        #endregion

        #region Properties

        public String Origin
        {
            get { return gamer; }
            set { gamer = value; }
        }

        public Boolean Enabled
        {
            get { return menuActive; }
        }
        #endregion

        #region Constructor
        public PauseMenuGUI()
        {
           /* pauseInputMap = new InputMap();
            int keyboardId = InputManager.Instance.FindDevice("keyboard");
            if (keyboardId >= 0)
            {
                pauseInputMap.BindAction(keyboardId, (int)Keys.Space, UnPause);
            }
            int gamepadId = InputManager.Instance.FindDevice("gamepad0");
            if (gamepadId >= 0)
            {
                pauseInputMap.BindAction(gamepadId, (int)XGamePadDevice.GamePadObjects.Start, UnPause);
                pauseInputMap.BindAction(gamepadId, (int)XGamePadDevice.GamePadObjects.Back, UnPause);
            }*/
            
            menuActive = false;
            GUIStyle stylePlayGUI = new GUIStyle();

            Name = "PauseMenu";
            Style = stylePlayGUI;
            Size = Game.Instance.ClientBounds;

            OnGUIWake = new OnWakeDelegate(StartPauseGUI);


        }


        #endregion

        #region Events
        public void StartPauseGUI(GUIControl control)
        {
            GUIBitmapStyle bitmapStyle = new GUIBitmapStyle();
            bitmapStyle.IsOpaque = false;
            //bitmapStyle.FillColor[CustomColor.ColorBase] = new Microsoft.Xna.Framework.Graphics.Color(0, 0, 0, 128);
            backgroundBitmap = new GUIBitmap();
            backgroundBitmap.Bitmap = @"data\images\ui\semigrey.png";
            backgroundBitmap.Style = bitmapStyle;
            backgroundBitmap.Position = new Vector2(0,0);
            backgroundBitmap.Size = Size;
            
            backgroundBitmap.Visible = false;

            backgroundBitmap.Folder = this;

        }

        private void UnPause(MenuItem menuItem)
        {
            UnPause(1.0f);
        }

        private void UnPause(float val)
        {
            if (val > 0)
            {
                if (Origin == Game.Network.Gamer.Gamertag)
                {
                    if (Game.Network != null)
                    {
                        Game.Network.HandleEvent(new GamePauseEvent(this, false, Game.Network.Gamer != null ? Game.Network.Gamer.Gamertag : String.Empty));
                    }
                }
            }
        }

        private void OnExit()
        {
            if (Game.Network != null)
            {
                Game.Network.HandleEvent(new GameQuitEvent(this, Game.Network.Gamer != null ? Game.Network.Gamer.Gamertag : String.Empty));
            }
        }

        private void OnExit(MenuItem menuItem)
        {
            OnExit();
        }

        private void OnExit(float val)
        {
            if (val > 0)
            {
                OnExit();
            }
        }

        #endregion

        #region Methods
        public void Init()
        {
            backgroundBitmap.Visible = false;
            menuActive = false;
            InitMenuManager();
        }

        private void InitMenuManager()
        {
            pauseMenu = new CircleMenu(0, 50.0f, "Pause Menu");

            MenuItem unPauseItem = new MenuItem("Resume");
            unPauseItem.MenuItemSelected = new MenuItem.MenuItemSelectedDelegate(UnPause);
            pauseMenu.AddItem(unPauseItem);

            MenuItem quitItem = new MenuItem("Quit");
            quitItem.MenuItemSelected = new MenuItem.MenuItemSelectedDelegate(OnExit);
            pauseMenu.AddItem(quitItem);

            menuManager = new MenuManager(Bounds.Center, new Vector2(0f, 0f));
            menuManager.CompletelyBackedOut = new MenuManager.CompletelyBackedOutDelegate(OnExit);

            menuManager.Visible = false;
        }

        public void EnablePauseScreen()
        {
            if (!menuActive)
            {
                menuManager.Current = pauseMenu;
                menuManager.Visible = true;

                
                backgroundBitmap.Visible = true;
                menuActive = true;
                TorqueEngineComponent.Instance.GameTimeScale = 0;
            }
        }

        public void DisablePauseScreen()
        {
            if (menuActive)
            {
                menuManager.Visible = false;
                menuManager.Current = null;
                backgroundBitmap.Visible = false;
                menuActive = false;
                TorqueEngineComponent.Instance.GameTimeScale = 1;
            }
        }
        #endregion

        public override void OnRender(Vector2 offset, GarageGames.Torque.MathUtil.RectangleF updateRect)
        {
            base.OnRender(offset, updateRect);
            menuManager.RefreshMenus();
        }
    }
}
